package com.sojpt.boot;

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
//gitee.com/sohnny/SoJpt-Boot.git
import org.springframework.context.annotation.Configuration;

import com.jfinal.core.Controller;

@Configuration
public class SpringUtil implements ApplicationContextAware, ApplicationContextInitializer<ConfigurableApplicationContext> {

    private static ApplicationContext applicationContext;

    public static Object getObject(String id) {
        Object object = null;
        object = applicationContext.getBean(id);
        return object;
    }
    public static <T> T getObject(Class<T> tClass) {
        return applicationContext.getBean(tClass);
    }

    public static Object getBean(String tClass) {
        return applicationContext.getBean(tClass);
    }

    public <T> T getBean(Class<T> tClass) {
        return applicationContext.getBean(tClass);
    }
    
    public static Map<String, ? extends Controller>  getController() {
    	return applicationContext.getBeansOfType(Controller.class);
    }

	@Override
	public void initialize(ConfigurableApplicationContext applicationContext) {
		SpringUtil.applicationContext = applicationContext;
	}
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringUtil.applicationContext = applicationContext;
	}

}
